package id.net.iconpln.bb.client.view;

import id.net.iconpln.bb.client.data.TestData;
import id.net.iconpln.bb.client.model.Stock;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.layout.FitData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.extjs.gxt.ui.client.widget.table.NumberCellRenderer;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.Element;

public class PageBbListBa extends LayoutContainer {
	private FormBinding formBindings;  
	private FormData formData; 
	private Grid<Stock> grid;
	
	  @Override  
	  protected void onRender(Element parent, int index) {  
	    super.onRender(parent, index);  
	    setLayout(new FitLayout());
	    
	    formData = new FormData("-20"); 
	    ContentPanel cp = new ContentPanel();  
	  
	    cp.setHeading("Rencana Jadwal Pengiriman Batubara");  
	    cp.setFrame(true);  
	    cp.setSize(900, 400);  
	    cp.setLayout(new RowLayout(Orientation.HORIZONTAL));  
	  
	    grid = createGrid();  
//	    grid.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);  
//	    grid.getSelectionModel().addListener(Events.SelectionChange,  
//	        new Listener<SelectionChangedEvent<Stock>>() {  
//	          public void handleEvent(SelectionChangedEvent<Stock> be) {  
//	            if (be.getSelection().size() > 0) {  
//	              formBindings.bind((ModelData) be.getSelection().get(0));  
//	            } else {  
//	              formBindings.unbind();  
//	            }  
//	          }  
//	        });  	     
//	  
//	    FormPanel panel = createForm();  
//	    formBindings = new FormBinding(panel, true);  
//	    formBindings.setStore((Store) grid.getStore());  
	  
//	    cp.add(panel, new RowData(.4, 1));
	    cp.add(grid, new RowData(1, 1));
//	    cp.add(grid);
	    
	    
//		setScrollMode(Scroll.AUTO);		
	    add(cp, new FitData(5));  
	  }  
	  
	  private FormPanel createForm() {  
	    FormPanel panel = new FormPanel();	    
	    panel.setHeaderVisible(false);
	    
	    FieldSet fieldSet = new FieldSet();  
	    fieldSet.setHeading("Data pengiriman");  
	    fieldSet.setCollapsible(false);
	    
	  
	    FormLayout layout = new FormLayout();  
	    layout.setLabelWidth(120);  
	    fieldSet.setLayout(layout);  

	    TextField<String> noPengiriman = new TextField<String>();  
	    noPengiriman.setName("noPengiriman");  
	    noPengiriman.setFieldLabel("No Pengiriman");    
	    fieldSet.add(noPengiriman, formData);	  	  
	    	     
	    TextField<String> noKontrak = new TextField<String>();	    
	    noKontrak.setName("noKontrak");  
	    noKontrak.setFieldLabel("No Kontrak");    
	    fieldSet.add(noKontrak, formData);

	    TextField<String> pemasok = new TextField<String>();	    
	    pemasok.setName("pemasok");  
	    pemasok.setFieldLabel("Nama Pemasok");    
	    fieldSet.add(pemasok, formData);	    	    

	    TextField<String> tonase = new TextField<String>();
	    tonase.setName("tonase");  
	    tonase.setFieldLabel("Vol Kirim (Ton)");	    
	    fieldSet.add(tonase, formData);

	    TextField<String> kualitas = new TextField<String>();
	    kualitas.setName("kualitas");  
	    kualitas.setFieldLabel("Kualitas (kalori)");	    
	    fieldSet.add(kualitas, formData);
	    	

	    panel.add(fieldSet);  	     

	    
	    FieldSet fieldSet2 = new FieldSet();  
	    fieldSet2.setHeading("ETA");  
	    fieldSet2.setCollapsible(false);
	    
	    FormLayout layout2 = new FormLayout();  
	    layout2.setLabelWidth(100);  
	    fieldSet2.setLayout(layout2);  	       	    	   

	    TextField<String> asal = new TextField<String>();	    
	    asal.setName("asalbb");  
	    asal.setFieldLabel("Asal batubara");    
	    fieldSet2.add(asal, formData);

	    
	    TextField<String> pltutj = new TextField<String>();	    
	    pltutj.setName("pltuj");  
	    pltutj.setFieldLabel("PLTU tujuan");    
	    fieldSet2.add(pltutj, formData);

	    
	    TextField<String> berangkat = new TextField<String>();
//	    DateField berangkat = new DateField();
	    berangkat.setName("berangkat");  
	    berangkat.setFieldLabel("Tgl kirim");    
	    fieldSet2.add(berangkat, formData);	    	    	    
	    
	    TextField<String> tiba = new TextField<String>();
//	    DateField tiba = new DateField();
	    tiba.setName("tiba");  
	    tiba.setFieldLabel("Tgl tiba");    
	    fieldSet2.add(tiba, formData);
	    
	    panel.add(fieldSet2);
	    
	    
	    FieldSet fieldSet3 = new FieldSet();  
	    fieldSet3.setHeading("Data Transportasi");  
	    fieldSet3.setCollapsible(false);

	    FormLayout layout3 = new FormLayout();  
	    layout3.setLabelWidth(160);  
	    fieldSet3.setLayout(layout3);  

	    
//	    CheckBox check1 = new CheckBox();  
//	    check1.setBoxLabel("Manifest");  
//	  
//	    CheckBox check2 = new CheckBox();  
//	    check2.setBoxLabel("Sertifikat kalori");  
//	    check2.setValue(true);  
//	  
//	    CheckBox check3 = new CheckBox();  
//	    check3.setBoxLabel("Sertifikat Weight");  
//	  
//	    CheckBoxGroup checkGroup = new CheckBoxGroup();  
//	    checkGroup.setFieldLabel("Lampiran");  
//	    checkGroup.add(check1);  
//	    checkGroup.add(check2);  
//	    checkGroup.add(check3);  
//	    fieldSet2.add(checkGroup, formData);
	    	    	   
	    TextField<String> namakpl = new TextField<String>();
	    namakpl.setName("namakpl");  
	    namakpl.setFieldLabel("Nama Kpl / Tongkang");	    
	    fieldSet3.add(namakpl, formData);
	    
	    TextField<String> pic = new TextField<String>();
	    pic.setName("pic");  
	    pic.setFieldLabel("PIC");	    
	    fieldSet3.add(pic, formData);
	    
	    TextField<String> notlp = new TextField<String>();
	    notlp.setName("notlp");  
	    notlp.setFieldLabel("No Tlp");	    
	    fieldSet3.add(notlp, formData);
	    
	    TextField<String> pkpl = new TextField<String>();
	    pkpl.setName("pkpl");  
	    pkpl.setFieldLabel("Pemilik Kpl / Tonkang");	    
	    fieldSet3.add(pkpl, formData);
	    
	    TextField<String> muatan = new TextField<String>();
	    muatan.setName("muatan");  
	    muatan.setFieldLabel("Kapsitas Kpl / tkg (Ton)");	    
	    fieldSet3.add(muatan, formData);
	  
	    TextField<String> jkpl = new TextField<String>();
	    jkpl.setName("jkpl");  
	    jkpl.setFieldLabel("Jml Kpl / Tkg");	    
	    fieldSet3.add(jkpl, formData);
	    
	    panel.add(fieldSet3);

	    ContentPanel p = new ContentPanel();
	    p.setHeaderVisible(false);	    
	    p.setButtonAlign(HorizontalAlignment.RIGHT);  
	    p.addButton(new Button("Reset", new SelectionListener<ButtonEvent>() {  
	      @Override  
	      public void componentSelected(ButtonEvent ce) {  
	        grid.getStore().rejectChanges();  
	      }  
	    }));  
	    p.addButton(new Button("Submit", new SelectionListener<ButtonEvent>() {  
	      @Override  
	      public void componentSelected(ButtonEvent ce) {  
	        grid.getStore().commitChanges();  
	      }  
	    }));  

	    panel.add(p);
	    
	    return panel;  
	  }  
	  
	  private Grid<Stock> createGrid() {  
	    final NumberFormat currency = NumberFormat.getCurrencyFormat();  
	    final NumberFormat number = NumberFormat.getFormat("0.00");  
	    final NumberCellRenderer<Grid<Stock>> numberRenderer = new NumberCellRenderer<Grid<Stock>>(  
	        currency);  
	  
	    GridCellRenderer<Stock> change = new GridCellRenderer<Stock>() {  
	  
	      public String render(Stock model, String property, ColumnData config, int rowIndex,  
	          int colIndex, ListStore<Stock> store, Grid<Stock> grid) {  
	        double val = (Double) model.get(property);  
	        String style = val < 0 ? "red" : "green";  
	        return "<span style='color:" + style + "'>" + number.format(val) + "</span>";  
	      }  
	    };  
	  
	    GridCellRenderer<Stock> gridNumber = new GridCellRenderer<Stock>() {  
	      public String render(Stock model, String property, ColumnData config, int rowIndex,  
	          int colIndex, ListStore<Stock> store, Grid<Stock> grid) {  
	        return numberRenderer.render(null, property, model.get(property));  
	      }  
	    };  
	  
	    List<ColumnConfig> configs = new ArrayList<ColumnConfig>();  

	    ColumnConfig column = new ColumnConfig();	    	     
	    
	    column = new ColumnConfig();  
	    column.setId("noPengiriman");  
	    column.setHeader("No Pengiriman");    
	    column.setWidth(150);    
	    configs.add(column);  

	    column = new ColumnConfig();  
	    column.setId("noKontrak");  
	    column.setHeader("No Kontrak");  
	    column.setWidth(100);  
	    configs.add(column);  	  
	    
	    column = new ColumnConfig();  
	    column.setId("noba");  
	    column.setHeader("No BA");  
	    column.setWidth(100);  
	    configs.add(column);
	    
	    column = new ColumnConfig();  
	    column.setId("tglba");  
	    column.setHeader("Tgl BA");    
	    column.setWidth(100);  
	    configs.add(column);

//	  
	    column = new ColumnConfig();  
	    column.setId("tonase");  
	    column.setHeader("Vol (Ton)");    
	    column.setWidth(100);    
	    configs.add(column);

	    	             	    
	    column = new ColumnConfig();  
	    column.setId("kualitas");  
	    column.setHeader("Kualitas (kal)");    
	    column.setWidth(150);  
	    configs.add(column);
	    
//	    
	    column = new ColumnConfig();  
	    column.setId("pltuj");  
	    column.setHeader("PLTU Tj");    
	    column.setWidth(150);  
	    configs.add(column);	    
	    
	    column = new ColumnConfig();  
	    column.setId("tiba");  
	    column.setHeader("Tgl Tiba");    
	    column.setWidth(100);  
	    configs.add(column);
	    
	    column = new ColumnConfig();  
	    column.setId("tglbongkar");  
	    column.setHeader("Tgl Bongkar");    
	    column.setWidth(75);   
	    configs.add(column);

	    column = new ColumnConfig();  
	    column.setId("tglfinishbongkar");  
	    column.setHeader("Tgl Selesai Bongkar");    
	    column.setWidth(75);   
	    configs.add(column);
	   	    	    
	    column = new ColumnConfig();  
	    column.setId("namakpl");  
	    column.setHeader("Nama Kpl / Tkg");    
	    column.setWidth(100);    
	    configs.add(column);
	   
	    column = new ColumnConfig();  
	    column.setId("pic");  
	    column.setHeader("PIC");    
	    column.setWidth(100);    
	    configs.add(column);
	    
	    column = new ColumnConfig();  
	    column.setId("jkpl");  
	    column.setHeader("Jml Kpl / Tkg");    
	    column.setWidth(100);    
	    configs.add(column);
	    
	    
	    ListStore<Stock> store = new ListStore<Stock>();  
	    store.setMonitorChanges(true);  
	    store.add(TestData.getJadwalkirim("x"));
	  
	    ColumnModel cm = new ColumnModel(configs);  
	  
	    Grid<Stock> grid = new Grid<Stock>(store, cm);  
	    grid.getView().setEmptyText("sdfd");  
	    grid.setBorders(false);  
//	    grid.setAutoExpandColumn("pemasok");  
	    grid.setBorders(true);  
	  
	    return grid;  
	  }  
}
